﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Security.Permissions;
using System.IO;

namespace OnSpaceSI
{
    [SecurityPermission(SecurityAction.LinkDemand)]
    public sealed partial class LicenseDisplayForm : Form
    { //version 1b
        private bool donotnavigate = true;
        private Screen targetscreen = null;
        private wbc iwbc = null;
        private string htmltext = null;
        private string outputstring = null;
        private string replacementtext = null;

        public bool Showing = false;

        public LicenseDisplayForm()
        {
            InitializeComponent();
            iwbc = new wbc(this);
            webBrowserHTMLDisplay.ObjectForScripting = iwbc;
            htmltext = GetLicenseHTMLString();
            Text = "DIRECTORY SYNCHRONIZER LICENSE AGREEMENT";
            Showing = true;
        }

        public void SetReplacementText(string argString)
        {
            donotnavigate = true;
            htmltext = htmltext.Replace("$REPLACEMENTTEXT$", argString);
            replacementtext = argString;
        }

        private void RTFDisplayForm_Load(object sender, EventArgs e)
        {
            if (htmltext == null)
            {
                MessageBox.Show("Error loading licensing agreement, please email On-Space Software Incorporated for support.", "Directory Synchronizer: Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
            if (targetscreen != null)
            {
                Left = targetscreen.Bounds.Left + 15;
                Top = targetscreen.Bounds.Top + 15;
                Height = targetscreen.Bounds.Height - 60;
            }
            webBrowserHTMLDisplay.DocumentText = htmltext;
        }
        private void webBrowserHTMLDisplay_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            if (donotnavigate)
            {
                donotnavigate = false;
                return;
            }
            Process.Start(e.Url.ToString());
            e.Cancel = true;
        }
        private void button_Click(object sender, EventArgs e)
        {
            Showing = false;
            Close();
        }

        public void ShowForReview()
        {
            buttonCancel.Visible = false;
            buttonAccept.Text = "OK";
            buttonAccept.Left = (Width / 2) - (buttonAccept.Width / 2);
        }

        public static string GetLicenseHTMLString()
        {
            try
            {
                string returnstring = null;
                System.Reflection.Assembly exeasm = System.Reflection.Assembly.GetExecutingAssembly();
                using (StreamReader reader = new StreamReader(exeasm.GetManifestResourceStream("DirectorySynchronizer.Resources.DirSyncEULA.html")))
                {
                    returnstring = reader.ReadToEnd();
                }
                return returnstring;
            }
            catch (ArgumentNullException)
            {
                return null;
            }
            catch (ArgumentException)
            {
                return null;
            }
            catch (FileLoadException)
            {
                return null;
            }
            catch (FileNotFoundException)
            {
                return null;
            }
            catch (BadImageFormatException)
            {
                return null;
            }
        }

        [ComVisible(true)]
        public class wbc
        {
            private LicenseDisplayForm ParentHTMLDisplayForm = null;

            public wbc(LicenseDisplayForm argParentHTMLDisplayForm)
            {
                ParentHTMLDisplayForm = argParentHTMLDisplayForm;
            }

            [ObfuscationAttribute(Exclude = true)]
            public void sh(string argString)
            {
                //the license code is imported here
                //don't forget to get user confirm
                ParentHTMLDisplayForm.outputstring = argString;
                MessageBox.Show(ParentHTMLDisplayForm.outputstring);
            }

            [ObfuscationAttribute(Exclude = true)]
            public void cl()
            {
                Clipboard.SetText(ParentHTMLDisplayForm.replacementtext);
            }
        }
    }
}
